package com.yanfan.zutai.controller;

import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.core.redis.RedisCache;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.zutai.aop.SysInitData;
import com.yanfan.zutai.aop.SysInitUpdateData;
import com.yanfan.zutai.aop.SysQueryData;
import com.yanfan.zutai.domain.BDeviceParameter;
import com.yanfan.zutai.domain.BDeviceRealData;
import com.yanfan.zutai.service.IBDeviceParameterService;
import com.yanfan.zutai.service.IBDeviceRealDataService;
import com.yanfan.zutai.util.Detect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备参数设置Controller
 *
 * @author ruoyi
 * @date 2021-04-06
 */
@RestController
@RequestMapping("/zutai/bDeviceParameter")
public class BDeviceParameterController extends BaseController {
    @Autowired
    private IBDeviceParameterService bDeviceParameterService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IBDeviceRealDataService bDeviceRealDataService;

    /**
     * 查询设备参数设置列表
     */
    @GetMapping("/list")
    //@SysQueryData
    public TableDataInfo list(BDeviceParameter bDeviceParameter) {
        startPage();
        List<BDeviceParameter> list = bDeviceParameterService.selectBDeviceParameterList(bDeviceParameter);
//        for (int i = 0; i < list.size(); i++) {
//            bDeviceParameter=list.get(i);
//            if(Detect.notEmpty(bDeviceParameter.getRealDataGuid())){
//                String[] split = bDeviceParameter.getRealDataGuid().split("-");
//                BDeviceRealData bDeviceRealData= new BDeviceRealData();
//                bDeviceRealData.setParamField(split[1]);
//                bDeviceRealData.setDeviceImei(split[0]);
//                List<BDeviceRealData> bDeviceRealDatas = bDeviceRealDataService.selectBDeviceRealDataList(bDeviceRealData);
//                if(bDeviceRealDatas.size()>0){
//                    bDeviceRealData = bDeviceRealDatas.get(0);
//                    redisService.setStr(bDeviceRealData.getDeviceImei()+"-"+bDeviceRealData.getParamName()+"-"+bDeviceParameter.getMappingNum(),bDeviceParameter.getMappingText(),null);
//                }
//            }
//        }
        return getDataTable(list);
    }

    /**
     * 导出设备参数设置列表
     */
    @Log(title = "设备参数设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @SysQueryData
    public AjaxResult export(BDeviceParameter bDeviceParameter) {
        List<BDeviceParameter> list = bDeviceParameterService.selectBDeviceParameterList(bDeviceParameter);
        ExcelUtil<BDeviceParameter> util = new ExcelUtil<BDeviceParameter>(BDeviceParameter.class);
        return util.exportExcel(list, "设备参数设置");
    }

    /**
     * 获取设备参数设置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bDeviceParameterService.selectBDeviceParameterById(id));
    }

    /**
     * 新增设备参数设置
     */
    @Log(title = "设备参数设置", businessType = BusinessType.INSERT)
    @PostMapping
    @SysInitData
    public AjaxResult add(@RequestBody BDeviceParameter bDeviceParameter) {
        if (Detect.notEmpty(bDeviceParameter.getRealDataGuid())) {
            String[] split = bDeviceParameter.getRealDataGuid().split("-");
            BDeviceRealData bDeviceRealData = new BDeviceRealData();
            bDeviceRealData.setParamField(split[1]);
            bDeviceRealData.setDeviceImei(split[0]);
            List<BDeviceRealData> bDeviceRealDatas = bDeviceRealDataService.selectBDeviceRealDataList(bDeviceRealData);
            if (bDeviceRealDatas.size() > 0) {
                bDeviceRealData = bDeviceRealDatas.get(0);
                redisCache.setCacheObject(bDeviceRealData.getDeviceImei() + "-" + bDeviceRealData.getParamName() + "-" + bDeviceParameter.getMappingNum(), bDeviceParameter.getMappingText());
            }
        }
        return toAjax(bDeviceParameterService.insertBDeviceParameter(bDeviceParameter));
    }

    /**
     * 修改设备参数设置
     */
    @Log(title = "设备参数设置", businessType = BusinessType.UPDATE)
    @PutMapping
    @SysInitUpdateData
    public AjaxResult edit(@RequestBody BDeviceParameter bDeviceParameter) {
        if (Detect.notEmpty(bDeviceParameter.getRealDataGuid())) {
            String[] split = bDeviceParameter.getRealDataGuid().split("-");
            BDeviceRealData bDeviceRealData = new BDeviceRealData();
            bDeviceRealData.setParamField(split[1]);
            bDeviceRealData.setDeviceImei(split[0]);
            List<BDeviceRealData> bDeviceRealDatas = bDeviceRealDataService.selectBDeviceRealDataList(bDeviceRealData);
            if (bDeviceRealDatas.size() > 0) {
                bDeviceRealData = bDeviceRealDatas.get(0);
                redisCache.setCacheObject(bDeviceRealData.getDeviceImei() + "-" + bDeviceRealData.getParamName() + "-" + bDeviceParameter.getMappingNum(), bDeviceParameter.getMappingText());
            }
        }
        return toAjax(bDeviceParameterService.updateBDeviceParameter(bDeviceParameter));
    }

    /**
     * 删除设备参数设置
     */
    @Log(title = "设备参数设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bDeviceParameterService.deleteBDeviceParameterByIds(ids));
    }
}
